package com.chushouya.order.rabbitmq.listener;

import com.general.framework.core.exception.BaseException;
import com.general.framework.core.exception.BusinessException;
import com.general.framework.core.exception.ViolationException;
import com.chushouya.common.dto.QueueMessage;
import com.chushouya.order.service.api.SuhuishouOrderApiService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 速回收订单同步消息监听器
 * 
 * @author chushouya
 * @date 2025-08-17
 */
@Component
@Slf4j
public class SuhuishouOrderListener {

    @Resource
    private SuhuishouOrderApiService suhuishouOrderApiService;

    @Resource
    private ObjectMapper objectMapper;

    /**
     * 监听速回收订单创建消息
     */
    @RabbitListener(queuesToDeclare = @Queue(name = "chushouya.suhuishou.order.create", durable = "true"))
    public void handleSuhuishouOrderMessage(QueueMessage<?> queueMessage) {
        try {
            log.info("收到速回收订单同步消息: messageId={}, data={}", queueMessage.getMessageId(), queueMessage.getData());
            
            // 验证消息数据
            if (queueMessage.getData() == null) {
                log.error("速回收订单同步消息数据为空: messageId={}", queueMessage.getMessageId());
                log.warn("数据为空的消息已处理，避免重复处理: messageId={}", queueMessage.getMessageId());
                return;
            }
            
            // 解析订单ID
            Long orderId = Long.valueOf(queueMessage.getData().toString());
            log.info("开始处理速回收订单同步，订单ID: {}, messageId: {}", orderId, queueMessage.getMessageId());
            
            // 处理速回收订单同步逻辑
            processSuhuishouOrderSync(orderId);
            
            log.info("速回收订单同步完成，订单ID: {}, messageId: {}", orderId, queueMessage.getMessageId());
            
        } catch (ViolationException e) {
            log.error("速回收订单同步消息验证错误: {}, messageId: {}", e.getMessage(), queueMessage.getMessageId());
            // 验证异常不抛出，避免消息重新入队
            log.warn("验证异常消息已处理，避免重复处理: messageId={}", queueMessage.getMessageId());
        } catch (BusinessException e) {
            log.error("速回收订单同步业务异常: {}, messageId: {}", e.getMessage(), queueMessage.getMessageId());
            // 业务异常不抛出，避免消息重新入队
            log.warn("业务异常消息已处理，避免重复处理: messageId={}", queueMessage.getMessageId());
        } catch (BaseException e) {
            log.error("速回收订单同步基础异常: {}, messageId: {}", e.getMessage(), queueMessage.getMessageId());
            // 基础异常不抛出，避免消息重新入队
            log.warn("基础异常消息已处理，避免重复处理: messageId={}", queueMessage.getMessageId());
        } catch (Exception e) {
            log.error("处理速回收订单同步消息异常: {}, messageId: {}", e.getMessage(), queueMessage.getMessageId(), e);
            // 其他异常不抛出，避免消息重新入队
            log.warn("异常消息已处理，避免重复处理: messageId={}", queueMessage.getMessageId());
        }
    }

    /**
     * 处理速回收订单同步业务逻辑
     * 
     * @param orderId 订单ID
     */
    private void processSuhuishouOrderSync(Long orderId) {
        // 参数校验
        if (orderId == null || orderId <= 0) {
            log.error("订单ID无效: {}", orderId);
            throw new IllegalArgumentException("订单ID无效");
        }
        
        suhuishouOrderApiService.createSuhuishouOrder(orderId);
        
        try {
            // 模拟API调用时间
            Thread.sleep(500);
        } catch (InterruptedException e) {
            log.error("订单同步被中断，订单ID: {}", orderId);
            Thread.currentThread().interrupt();
            throw new RuntimeException("订单同步被中断", e);
        }
        
        log.info("订单同步到速回收完成，订单ID: {}", orderId);
    }
}
